package com.example.pingtools.adpater

import android.content.Context
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.SimpleAdapter
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat.getColor
import androidx.recyclerview.widget.RecyclerView
import com.example.pingtools.R
import com.example.pingtools.bean.IpDataBean
import com.example.pingtools.bean.ipbean
import com.example.pingtools.util.PingUtil
import kotlinx.android.synthetic.main.layout_addip.*
import kotlinx.coroutines.*
import java.lang.Exception
import java.net.ConnectException
import java.net.InetSocketAddress
import java.net.Socket

class IpItemAd(context: Context,private var textList: List<IpDataBean>) :
    RecyclerView.Adapter<IpItemAd.MyViewHolder>() {
    var context1: Context=context
    private var itemClickListener: IKotlinItemClickListener? = null
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_ip, parent, false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int = textList.size ?: 0

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onBindViewHolder(holder: IpItemAd.MyViewHolder, position: Int) {

        var textpos = textList[position]
        // holder.title.text = textpos
        holder.tvbz.text=textpos.text
        holder.tvip.text="IP:"+textpos.ip
        holder.tvport.text="端口:"+textpos.port
        var num=position+1
        holder.tvnum.text="("+num+")"
        Log.d("fffffffffffff",""+position)
        var isping= PingUtil().startPing(textpos.ip)
        if(isping){
            holder.pingonlin.text="Ping: 连接正常"
            //isPortOpen(textpos.ip,textpos.port.toInt(),3000,holder)
            holder.pingonlin.setTextColor(context1.getColor(R.color.green));
        }else{
            holder.pingonlin.text="Ping: 连接失败"
            holder.pingonlin.setTextColor(context1.getColor(R.color.red));
        }
        var isopen: Boolean = false
        CoroutineScope(Dispatchers.IO).async {
            try {
                // Log.d("rrrrrrrrr", "$ip $port")
                val socket = Socket()
                socket.connect(InetSocketAddress(textpos.ip, textpos.port.toInt()),2000)
                socket.close()
                isopen = true
            } catch (ce: ConnectException) {
                ce.printStackTrace()
                // Log.d("rrrrrrrrr", "1")
                isopen = false
            } catch (ex: Exception) {
                ex.printStackTrace()
                // Log.d("rrrrrrrrr", "2")
                isopen = false
            }
            CoroutineScope(Dispatchers.Main).async {
                if (isopen) {
                    holder.tvopenport!!.text = "端口:开放"
                    holder.tvopenport!!.setTextColor(context1.getColor(R.color.green));

                } else {
                    holder.tvopenport!!.text = "端口:未开放"
                    holder.tvopenport!!.setTextColor(context1.getColor(R.color.red));
                }

            }

        }
        holder.tvdel.setOnClickListener {
            itemClickListener!!.iponItemClickListenerdel(position)
        }
        holder.tvupdate.setOnClickListener {
            itemClickListener!!.iponItemClickListener(position)
        }

    }



    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvbz: TextView = itemView.findViewById(R.id.tv_bz)
        var tvip: TextView = itemView.findViewById(R.id.tv_ip)
        var tvport: TextView = itemView.findViewById(R.id.tv_port)
        var pingonlin: TextView = itemView.findViewById(R.id.ping_onlin)
        var tvopenport: TextView = itemView.findViewById(R.id.tv_openport)
        var tvdel: TextView = itemView.findViewById(R.id.tv_del)
        var tvcontent:LinearLayout=itemView.findViewById(R.id.tv_ipcontent)
        var tvupdate:TextView=itemView.findViewById(R.id.tv_update)
        var tvnum:TextView=itemView.findViewById(R.id.tv_num)
    }
    // 提供set方法
    fun setOnKotlinItemClickListener(itemClickListener: IKotlinItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    //自定义接口
    interface IKotlinItemClickListener {
      fun iponItemClickListener(position: Int)
      fun iponItemClickListenerdel(position: Int)
    }

}